﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModuleITDesk.DAL
{
    public class DALProject
    {
        private ModuleITDeskEntities contextDB;
        public DALProject()
        {
            contextDB=new ModuleITDeskEntities();
        }
        public List<Project> GetProjectListByUserId(int userid)
        {
            try
            {
                List<Project> temp = contextDB.Projects.Where(p => p.UserID == userid).ToList();
                    return temp.Count > 0 ? temp : new List<Project>();
            }
            catch 
            {
                return new List<Project>();
            }
        }
        public List<Project> GetProjectList()
        {
            try
            {
                var temp = contextDB.Projects;
                return temp.ToList().Count > 0 ? temp.ToList() : null;
            }
            catch
            { return null; }
        }
        public bool InsertProject(Project project)
        {
            try
            {
                contextDB.Projects.Add(project);
                contextDB.SaveChanges();
                return true;
            }
            catch { return false; }
        }
        public bool UpdateProject(Project project)
        {
            try
            {
                Project existing = contextDB.Projects.Find(project.ProjectID);
                ((IObjectContextAdapter)contextDB).ObjectContext.Detach(existing);
                contextDB.Entry(project).State = EntityState.Modified;
                contextDB.SaveChanges();
                return true;
            }
            catch { return false; }
        }
        public bool DeleteProject(Project project)
        {
            try
            {
                Project existing = contextDB.Projects.Find(project.ProjectID);
                ((IObjectContextAdapter)contextDB).ObjectContext.Detach(existing);
                contextDB.Entry(project).State = EntityState.Deleted;
                contextDB.SaveChanges();
                return true;
            }
            catch { return false; }
        }
    }
}
